﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace MOVIE_THEATER
{
    public partial class BANVE_THAYDOIGIAVE : Form
    {
        public BANVE_THAYDOIGIAVE()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmndlabel_Click(object sender, EventArgs e)
        {

        }

        private void BANVE_THAYDOIGIAVE_Load(object sender, EventArgs e)
        {
            this.PhimcomboBox.DataSource = SqlDataAccessHelper.ExecuteQuery("select tenphim, maphim from phim");
            this.PhimcomboBox.DisplayMember = "tenPhim";
            this.PhimcomboBox.ValueMember = "maphim";

            //load gio chieu phim
            string maphimselected = this.PhimcomboBox.SelectedValue.ToString();
           
            this.ThoiGianChieucomboBox.DataSource = SqlDataAccessHelper.ExecuteQuery("select sc.thoigianbatdau , ct.mactsuatchieu from phim p join ctsuatchieu ct on p.maphim = ct.maphim join suatchieu sc on sc.masuatchieu = ct.masuatchieu where p.maphim = '" + maphimselected + "'");
            this.ThoiGianChieucomboBox.DisplayMember = "thoigianbatdau";
            this.ThoiGianChieucomboBox.ValueMember = "mactsuatchieu";

            //load danh sach ve chua mua
            string mactsuatchieuSelected = "";

            if (this.ThoiGianChieucomboBox.SelectedValue != null)
            {
                mactsuatchieuSelected = this.ThoiGianChieucomboBox.SelectedValue.ToString();
                this.danhSachVedataGridView1.DataSource = SqlDataAccessHelper.ExecuteQuery("select v.nguoilon_treem, v.mave , v.tinhtrangve,  dg.dayghe, dg.soghe, v.sdt, v.cmnd, v.hotenkh from ve v join dayghe_phongchieu dg on v.madaypc = dg.madaypc where v.tinhtrangve != 2 and v.MaCTSuatChieu = '" + mactsuatchieuSelected + "'");
                for (int i = 2; i < int.Parse(this.danhSachVedataGridView1.Columns.Count.ToString()); i++)
                    this.danhSachVedataGridView1.Columns[i].ReadOnly = true;
            }
            //
        }

        private void PhimcomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
           // load gio chieu phim
            string maphimselected = this.PhimcomboBox.SelectedValue.ToString();

            this.ThoiGianChieucomboBox.DataSource = SqlDataAccessHelper.ExecuteQuery("select sc.thoigianbatdau , ct.mactsuatchieu from phim p join ctsuatchieu ct on p.maphim = ct.maphim join suatchieu sc on sc.masuatchieu = ct.masuatchieu where p.maphim = '" + maphimselected + "'");
            this.ThoiGianChieucomboBox.DisplayMember = "thoigianbatdau";
            this.ThoiGianChieucomboBox.ValueMember = "mactsuatchieu";
            //load danh sach ve chua mua
            string mactsuatchieuSelected = "";

            if (this.ThoiGianChieucomboBox.SelectedValue != null)
            {
                mactsuatchieuSelected = this.ThoiGianChieucomboBox.SelectedValue.ToString();
                this.danhSachVedataGridView1.DataSource = SqlDataAccessHelper.ExecuteQuery("select v.nguoilon_treem, v.mave , v.tinhtrangve, dg.dayghe, dg.soghe, v.sdt, v.cmnd, v.hotenkh from ve v join dayghe_phongchieu dg on v.madaypc = dg.madaypc where v.tinhtrangve != 2 and v.MaCTSuatChieu = '" + mactsuatchieuSelected + "'");
                for (int i = 2; i < int.Parse(this.danhSachVedataGridView1.Columns.Count.ToString()); i++)
                    this.danhSachVedataGridView1.Columns[i].ReadOnly = true;
            }
            
        }

        private void ThoiGianChieucomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string mactsuatchieuSelected = "";
            try
            {

                if (this.ThoiGianChieucomboBox.SelectedValue != null)
                {
                    mactsuatchieuSelected = this.ThoiGianChieucomboBox.SelectedValue.ToString();
                    this.danhSachVedataGridView1.DataSource = SqlDataAccessHelper.ExecuteQuery("select v.nguoilon_treem, v.mave , v.tinhtrangve, dg.dayghe, dg.soghe, v.sdt, v.cmnd, v.hotenkh from ve v join dayghe_phongchieu dg on v.madaypc = dg.madaypc where v.tinhtrangve != 2 and v.MaCTSuatChieu = '" + mactsuatchieuSelected + "'");
                    for (int i = 2; i < int.Parse(this.danhSachVedataGridView1.Columns.Count.ToString()); i++)
                        this.danhSachVedataGridView1.Columns[i].ReadOnly = true;
                }
            }
            catch (Exception ex)
            {
               
            }
        }

        private void MuaErrorbutton1_Click(object sender, EventArgs e)
        {
            int tongtien = 0;

            int rows = int.Parse(this.danhSachVedataGridView1.RowCount.ToString());
            for (int i = 0; i < rows; i++)
            {

                if (this.danhSachVedataGridView1.Rows[i].Cells[0].Value != null)
                {

                    SqlConnection connect = new SqlConnection("Data Source=localhost ;Initial Catalog=QLRAPPHIM;Integrated Security=True");

                    connect.Open();

                    SqlCommand comm = new SqlCommand();

                    comm.Connection = connect;



                    string maVe = this.danhSachVedataGridView1.Rows[i].Cells[2].Value.ToString();
                    bool loaiNguoi = false;
                    if (bool.Parse(this.danhSachVedataGridView1.Rows[i].Cells[1].Value.ToString()) == true)
                    {

                        loaiNguoi = true;
                    }

                    comm.CommandText = "sp_banve_error";
                    comm.CommandType = CommandType.StoredProcedure;
                    comm.Parameters.Add(new SqlParameter("@MaVe", SqlDbType.VarChar, 10));
                    comm.Parameters["@MaVe"].Direction = ParameterDirection.Input;
                    comm.Parameters["@MaVe"].Value = maVe;

                    comm.Parameters.Add(new SqlParameter("@NguoiLon", SqlDbType.Bit));
                    comm.Parameters["@NguoiLon"].Direction = ParameterDirection.Input;
                    comm.Parameters["@NguoiLon"].Value = loaiNguoi;

                    comm.Parameters.Add(new SqlParameter("@TongTien", SqlDbType.Int));
                    comm.Parameters["@TongTien"].Direction = ParameterDirection.Output;
                    comm.Parameters["@TongTien"].Value = "";

                    comm.ExecuteNonQuery();
                    int result = int.Parse(comm.Parameters["@tongTien"].Value.ToString());
                    tongtien += result;

                    comm.Dispose();

                }
            }


            //LOAD LAI DU LIEU
            string mactsuatchieuSelected = "";
            try
            {

                if (this.ThoiGianChieucomboBox.SelectedValue != null)
                {
                    mactsuatchieuSelected = this.ThoiGianChieucomboBox.SelectedValue.ToString();
                    this.danhSachVedataGridView1.DataSource = SqlDataAccessHelper.ExecuteQuery("select v.nguoilon_treem, v.mave , v.tinhtrangve, dg.dayghe, dg.soghe, v.sdt, v.cmnd, v.hotenkh from ve v join dayghe_phongchieu dg on v.madaypc = dg.madaypc where v.tinhtrangve != 2 and v.MaCTSuatChieu = '" + mactsuatchieuSelected + "'");
                    for (int i = 2; i < int.Parse(this.danhSachVedataGridView1.Columns.Count.ToString()); i++)
                        this.danhSachVedataGridView1.Columns[i].ReadOnly = true;
                }
            }
            catch (Exception ex)
            {

            }

            MessageBox.Show("tổng tiền thanh toán: " + tongtien);

        }

        private void MuaFixButton_Click(object sender, EventArgs e)
        {
            int tongtien = 0;

            int rows = int.Parse(this.danhSachVedataGridView1.RowCount.ToString());
            for (int i = 0; i < rows; i++)
            {

                if (this.danhSachVedataGridView1.Rows[i].Cells[0].Value != null)
                {

                    SqlConnection connect = new SqlConnection("Data Source=localhost ;Initial Catalog=QLRAPPHIM;Integrated Security=True");

                    connect.Open();

                    SqlCommand comm = new SqlCommand();

                    comm.Connection = connect;



                    string maVe = this.danhSachVedataGridView1.Rows[i].Cells[2].Value.ToString();
                    bool loaiNguoi = false;
                    if (bool.Parse(this.danhSachVedataGridView1.Rows[i].Cells[1].Value.ToString()) == true)
                    {

                        loaiNguoi = true;
                    }

                    comm.CommandText = "sp_banve_fix";
                    comm.CommandType = CommandType.StoredProcedure;
                    comm.Parameters.Add(new SqlParameter("@MaVe", SqlDbType.VarChar, 10));
                    comm.Parameters["@MaVe"].Direction = ParameterDirection.Input;
                    comm.Parameters["@MaVe"].Value = maVe;

                    comm.Parameters.Add(new SqlParameter("@NguoiLon", SqlDbType.Bit));
                    comm.Parameters["@NguoiLon"].Direction = ParameterDirection.Input;
                    comm.Parameters["@NguoiLon"].Value = loaiNguoi;

                    comm.Parameters.Add(new SqlParameter("@TongTien", SqlDbType.Int));
                    comm.Parameters["@TongTien"].Direction = ParameterDirection.Output;
                    comm.Parameters["@TongTien"].Value = "";

                    comm.ExecuteNonQuery();
                    int result = int.Parse(comm.Parameters["@tongTien"].Value.ToString());
                    tongtien += result;

                    comm.Dispose();

                }
            }


            //LOAD LAI DU LIEU
            string mactsuatchieuSelected = "";
            try
            {

                if (this.ThoiGianChieucomboBox.SelectedValue != null)
                {
                    mactsuatchieuSelected = this.ThoiGianChieucomboBox.SelectedValue.ToString();
                    this.danhSachVedataGridView1.DataSource = SqlDataAccessHelper.ExecuteQuery("select v.nguoilon_treem, v.mave , v.tinhtrangve, dg.dayghe, dg.soghe, v.sdt, v.cmnd, v.hotenkh from ve v join dayghe_phongchieu dg on v.madaypc = dg.madaypc where v.tinhtrangve != 2 and v.MaCTSuatChieu = '" + mactsuatchieuSelected + "'");
                    for (int i = 2; i < int.Parse(this.danhSachVedataGridView1.Columns.Count.ToString()); i++)
                        this.danhSachVedataGridView1.Columns[i].ReadOnly = true;
                }
            }
            catch (Exception ex)
            {

            }

            MessageBox.Show("tổng tiền thanh toán: " + tongtien);
        }
    }
}
